<?php
include_once '../includes/common.php';
$proid=intval($_GET['proid']);
$url=daddslashes($_GET['url']);
$authcode=daddslashes($_GET['authcode']);
$program = $DB->get_row("SELECT * FROM authguao_program WHERE id='$proid' limit 1");
$siterow = $DB->get_row("SELECT * FROM authguao_site WHERE proid='$proid' and url='$url' limit 1");
if(!$siterow){
    $url = "*.".get_host($url);
    $siterow = $DB->get_row("SELECT * FROM authguao_site WHERE proid='$proid' and url='*.".get_host($url)."' limit 1");
}
if (!$proid) {
    exit('{"code":0,"msg":"程序识别码错误！"}');
} else if (strlen($authcode)!=32 || !$authcode) {
    exit('{"code":0,"msg":"授权码错误！"}');
} else if (!$program) {
    exit('{"code":0,"msg":"该程序不存在，可能已经被下架！"}');
} else if ($authcode != $siterow['authcode']) {
    exit('{"code":0,"msg":"授权KEY验证失败，请检查配置！"}');
} else if ($siterow['active'] != 1) {
    exit('{"code":0,"msg":"很抱歉您的站点已被禁封！"}');
} else {
    if ($conf['active_program'] == 1) {
        if ($program['active'] == 0) {
            exit('{"code":0,"msg":"很抱歉该程序已停止运行！"}');
        }
    } else {
        $param = base64_encode(authcode($proid."|".$url."|".$authcode."|".(TIMESTAMP+600),'ENCODE','authguaos!!'));
        $download = $authurl.'api/downloads.php?release=true&param='.$param.'&rand='.rand(100000,999999);
        exit('{"code":1,"url":"'.$download.'"}'); 
    }
}